package org.jeecg.modules.demo.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.member.entity.JkMemberInfo;
import org.jeecg.modules.demo.member.mapper.JkMemberInfoMapper;
import org.jeecg.modules.demo.member.service.IJkMemberInfoService;
import org.jeecg.modules.system.entity.SysTenant;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.jeecg.modules.base.service.BaseCommonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: jk_member_info
 * @Author: jeecg-boot
 * @Date:   2025-05-18
 * @Version: V1.0
 */
@Service
public class JkMemberInfoServiceImpl extends ServiceImpl<JkMemberInfoMapper, JkMemberInfo> implements IJkMemberInfoService {

    @Resource
    private BaseCommonService baseCommonService;

    @Autowired
    private JkMemberInfoMapper userMapper;



    /**
     * 校验用户是否有效
     * @param memberUser
     * @return
     */
    @Override
    public Result<?> checkUserIsEffective(JkMemberInfo memberUser) {
        Result<?> result = new Result<Object>();
        //情况1：根据用户信息查询，该用户不存在
        if (memberUser == null) {
            result.error500("该用户不存在，请注册");
            baseCommonService.addLog("用户登录失败，用户不存在！", CommonConstant.LOG_TYPE_1, null);
            return result;
        }

        //情况2：根据用户信息的状态
        if (CommonConstant.USER_FREEZE.equals(memberUser.getIsEnabled())) {
            baseCommonService.addLog("用户登录失败，用户名:" + memberUser.getUsername() + "已封号！", CommonConstant.LOG_TYPE_1, null);
            result.error500("该用户已封号");
            return result;
        }
        return result;
    }


    @Override
    public JkMemberInfo getUserByName(String username) {
        JkMemberInfo sysUser = userMapper.getUserByName(username);
        return sysUser;
    }

    @Override
    public JkMemberInfo getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }


    @Override
    public JkMemberInfo getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(JkMemberInfo user) {
        userMapper.insert(user);
    }

}
